<?php

namespace App\Console\Commands;

use App\Models\Base\LogPlayerHourNumber;
use App\Service\AccountService;
use Carbon\Carbon;
use Illuminate\Console\Command;

class FetchHourPlayer extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'fetch:hour';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '每小时获取登录玩家数量';

    /**
     * Create a new command instance.
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $carbon_now = Carbon::now();
        $hour = $carbon_now->format('H');
        $date_time = $carbon_now->toDateString();

        if ('00' == $hour) {
            $hour = 23;
            $date_time = Carbon::yesterday()->toDateString();
        }

        $now_hour = $carbon_now->timestamp;
        $one_hour_ago = $carbon_now->subHours(1)->timestamp;
        $count = AccountService::getHourPlayerNum($one_hour_ago, $now_hour);
        $model = new LogPlayerHourNumber();
        $model->date_time = $date_time;
        $model->hour = $hour;
        $model->number = $count;
        $model->save();
    }
}
